﻿using System.Windows;
using System.Windows.Media;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Threading;

namespace WpfApplication2
{   

    public partial class MainWindow : Window
    {
        #region "Properties"
        bool ctrl = false;
        PlayerView playerView = null;
        LibraryView libraryView = null;
        #endregion

        public MainWindow()
        {
            InitializeComponent();
            playerView = new PlayerView(this);
            libraryView = new LibraryView(this);

            goToPlayer();
            goToLibrary();

            Width = 890;
            Height = 380;

            playerView.isActive = false;
        }

        public void goToLibrary()
        {
            libView.NavigationService.Navigate(libraryView);
            librarySettings();
        }

        public void goToPlayer()
        {
            playView.NavigationService.Navigate(playerView);
            playerSettings();
        }

        private void librarySettings()
        {
        }

        private void playerSettings()
        {
        }

        #region "Event"
        private void keyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.LeftCtrl)
                ctrl = true;
        }

        private void keyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.LeftCtrl)
                ctrl = false;

            if (e.Key == Key.Escape)
                playerView.fullScreen();
            else if (e.Key == Key.Space)
                playerView.interrupt();
            else if (e.Key == Key.S && ctrl == true)
                playerView.stop();
            else if (e.Key == Key.P && ctrl == true)
                playerView.interrupt();
            else if (e.Key == Key.Back && ctrl == true)
                playerView.lecteur.SpeedRatio = 1;
            else if (e.Key == Key.OemPlus && ctrl == true)
                playerView.lecteur.SpeedRatio += 0.2;
            else if (e.Key == Key.OemOpenBrackets && ctrl == true)
                playerView.lecteur.SpeedRatio -= 0.2;
        }

        public void ticktock(object sender, EventArgs e)
        {
            try
            {
                    if (playerView.clickDown == false && playerView.pause == false)
                    {
                        timeline.Value = playerView.lecteur.Position.TotalSeconds;
                        lbl_time.Content = playerView.lecteur.Position.ToString();
                    }
            }
            catch (Exception ex)
            {
            }
        }

        private void timeline_dragStarted(object sender, DragStartedEventArgs e)
        {
            playerView.clickDown = true;
        }

        private void timeline_dragCompleted(object sender, DragCompletedEventArgs e)
        {
            try
            {
                int pos = Convert.ToInt32(timeline.Value);

                playerView.lecteur.Position = new TimeSpan(0, 0, 0, pos, 0);

                playerView.clickDown = false;
            }
            catch (Exception ex)
            {
            }
        }

        private void trackwave_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            try
            {
                playerView.lecteur.Volume = (double)trackwave.Value;
            }
            catch (Exception ex)
            {
            }
        }

        private void Btn_FullScreen_Click(object sender, RoutedEventArgs e)
        {
            playerView.fullScreen();
        }

        private void Btn_Volume_Click(object sender, RoutedEventArgs e)
        {
            playerView.mute = !playerView.mute;

            if (playerView.mute == true)
            {
                volumeImage.Source = new BitmapImage(new Uri("../../Images/sound_empty_icon.png", UriKind.Relative));
                playerView.stopVolume();
            }
            else
            {
                volumeImage.Source = new BitmapImage(new Uri("../../Images/sound_full_icon.png", UriKind.Relative));
                playerView.resetVolume();
            }
        }

        private void Btn_Parcourir_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog browseWindow = new Microsoft.Win32.OpenFileDialog();

            browseWindow.Title = "Choisissez votre musique";

            Nullable<bool> result = browseWindow.ShowDialog();
            if (result == true)
            {
                try
                {
                    try
                    {
                        playerView.lecteur.LoadedBehavior = MediaState.Stop;
                        playerView.lecteur.ClearValue(MediaElement.SourceProperty);


                        playerView.chemin = browseWindow.FileName;
                        playerView.lecteur.Source = new Uri(playerView.chemin);

                        playerView.lecture = true;
                        playerView.pause = true;

                    }
                    catch (Exception ex)
                    {
                    }
                }
                catch (Exception ex2)
                {
                }
            }
        }

        private void Btn_Forward_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                playerView.endMedia();
                libraryView.playNextMedia();
            }
            catch (Exception ex)
            {
            }
        }

        private void Btn_Arret_Click(object sender, RoutedEventArgs e)
        {
            playerView.stop();
        }

        private void Btn_Backward_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                playerView.endMedia();
                libraryView.playPreviousMedia();
            }
            catch (Exception ex)
            {
            }
        }

        private void Btn_interrupt_Click(object sender, RoutedEventArgs e)
        {
            playerView.interrupt();
        }

        private void Btn_LibraryView_Click(object sender, RoutedEventArgs e)
        {
            if (playerView.isActive == false)
            {
                playView.Visibility = Visibility.Visible;
                libView.Visibility = Visibility.Hidden;
                playerView.isActive = true;
            }
            else
            {
                playView.Visibility = Visibility.Hidden;
                libView.Visibility = Visibility.Visible;
                playerView.isActive = false;
            }
        }

        private void sliderToPos(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                int pos = Convert.ToInt32(timeline.Value);

                playerView.lecteur.Position = new TimeSpan(0, 0, 0, pos, 0);
            }
            catch (Exception ex)
            {
            }

        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double newWidth = e.NewSize.Width;
            double newHeight = e.NewSize.Height;

            if ((newWidth - 98) > 0)
                libraryView.MediaGrid.Height = newHeight - 98;

            double leftTreeViewWidth = newWidth / 7;
            double rightTreeViewWidth = newWidth / 4;

            libraryView.leftTreeView.Width = leftTreeViewWidth;
            libraryView.rightTreeView.Width = rightTreeViewWidth;
            libraryView.MediaGrid.Width = newWidth - (leftTreeViewWidth + rightTreeViewWidth);

        }
        #endregion

        #region "Media"
        public void playMediaWithPath(String path)
        {
            if (String.IsNullOrEmpty(path) == false)
            {
                    playerView.lecteur.LoadedBehavior = MediaState.Stop;
                    playerView.lecteur.ClearValue(MediaElement.SourceProperty);

                    playerView.lecture = true;
                    playerView.pause = true;

                    playerView.chemin = path;
                    playerView.lecteur.Source = new Uri(playerView.chemin);
            }
        }

        public void nextMedia()
        {
            libraryView.playNextMedia();
        }
        #endregion
    }
}
